<?php

class Mpage_model extends Model {
	
	private $table 	= 'mpage';
	
	function Mpage() {
		parent::Model ();
	}
	
	function write_mpage() {
		
		$data = array(
			'id_lang'		=> get_admin_lang(),
			'name'			=> isset( $_POST['name'] ) ? htmlspecialchars( $_POST['name'] ) : '',
			'description'	=> isset( $_POST['description'] ) ? $_POST['description'] : '',
			'url_text'		=> isset( $_POST['link'] ) ? htmlspecialchars( $_POST['link'] ) : '',
			'orders'		=> 0,	
			'status'		=> isset( $_POST['status'] ) ? (int)$_POST['status'] : '',			
			'date'			=> time()
		);
		
		$this->db->insert( $this->table , $data );		
	}
	
	function update_mpage() {
		
		$data = array(
			'name'			=> isset( $_POST['name'] ) ? htmlspecialchars( $_POST['name'] ) : '',
			'description'	=> isset( $_POST['description'] ) ? $_POST['description'] : '',
			'url_text'		=> isset( $_POST['link'] ) ? htmlspecialchars( $_POST['link'] ) : '',
			'status'		=> isset( $_POST['status'] ) ? (int)$_POST['status'] : '',
		);
		
		$id = isset( $_POST['hid'] ) ? $_POST['hid'] : 0;
		
		$this->db->where('id', $id );
		$this->db->where('id_lang', get_admin_lang() );
		
		$this->db->update( $this->table , $data );		
	}
	
	function list_mpage( $num , $offset, &$total ) {
		$data = array ();
		$total = '';		
		$query = $this->db->query("SELECT `id`, `name`, `url_text`, `status`, `orders` FROM $this->table WHERE `id_lang` = '".get_admin_lang()."' ORDER BY `orders` ASC, `id` DESC LIMIT $offset, $num");
		
		if ( $query->num_rows () > 0 ) {
			$data = $query->result_array();
		}
		
		// Tông số bản ghi
		$query = $this->db->query("SELECT `id` FROM $this->table WHERE `id_lang` = '".get_admin_lang()."' ");
		$total = $query->num_rows();
		
		$query->free_result ();
		return $data;
	}
	
	function select_mpage( $id ) {
		$data = array ();
		
		if ( is_numeric( $id ) ) {
			$query = $this->db->query("SELECT `id`, `name`, `description`, `url_text`, `orders`, `status` FROM $this->table WHERE `id` = '$id' AND `id_lang` = '".get_admin_lang()."' LIMIT 1 ");
			
			if ( $query->num_rows () > 0 ) 
				$data = $query->row_array();
			
			$query->free_result ();
		}
		return $data;
	}
	
	function update_mpage_status( $total = 0 ) {
	
		for( $k = 1; $k < $total ; $k++ ) {
			
			$status = @(int)$_POST['status_'.$k];
			$order 	= @(int)$_POST['order_'.$k];
			$id		= @(int)$_POST['id_'.$k]; 
			
			$data = array(				
				'orders'	=> $order,
				'status'	=> $status
			);
			
			$this->db->where('id', $id );	
			$this->db->where('id_lang', get_admin_lang() );
			
			$this->db->update( $this->table, $data);
		}
	}
	
	function del_mpage( $id = 0 ) {		
		$this->db->where('id', $id );
		$this->db->where('id_lang', get_admin_lang() );
		$this->db->delete( $this->table );		 
	}
	
}
?>